import { computerCss, computerWidthHeightTopLeft } from './unit.js'
import { send } from '../index.js' // 发送websocket的函数
import $ from 'jquery'

function Label(data) {
  console.log('data: ', data)
  computerWidthHeightTopLeft(data)
  var bindingsArr = []
  if (data.bindings && data.bindings.length != 0) {
    data.bindings.map((item) => {
      bindingsArr.push({
        id: item.id,
        ord: item.ord,
        items: item.items,
      })
    })
  }
  bindingsArr = JSON.stringify(bindingsArr)
  let dom = ''
  dom +=
    '<div id=' +
    data.id +
    ' class="autoSubscribe" data=' +
    bindingsArr +
    ' style=position:absolute;overflow:hidden;' +
    computerCss(
      [
        {
          width: 'width',
          height: 'height',
          left: 'left',
          top: 'top',
        },
      ],
      data,
      'px'
    ) +
    buildPropertyNiagaraToCss(data) +
    '>'
  dom += '<span>' + data.text + '</span>'
  dom += '</div>'
  //
  return dom
}

function buildPropertyNiagaraToCss(data) {
  // data.image = "http://192.168.1.190:8848/ord?file:^22.1K.png";
  var styleStr = ''
  if (data.image != 'null') {
    styleStr +=
      "background-image:url('" +
      data.image +
      "');" +
      'background-repeat:no-repeat;background-position:right;'
  }
  if (data.font != 'null') {
    if (data.font.indexOf('bold') != -1) {
      styleStr += 'font-weight:bold;'
    }
    if (data.font.indexOf('italic') != -1) {
      styleStr += 'font-style:italic;'
    }
    if (data.font.indexOf('underline') != -1) {
      styleStr += 'text-decoration:underline;'
    }
    if (data.font.replace(/[^\d.]/g, '')) {
      styleStr += 'font-size:' + data.font.replace(/[^\d.]/g, '') + 'px;'
    }
    if (data.font.split('pt ')[1]) {
      styleStr +=
        'font-family:' + data.font.split('pt ')[1].replace(' ', '&nbsp;') + ';'
    }
  }
  return styleStr
}

function LabelEvent(data) {
  if (data.bindings && data.bindings.length != 0) {
    var endData = []
    data.bindings.map((item) => {
      endData.push({
        id: data.id,
        ord: item.ord,
        class: 'label',
      })
    })

    send({
      type: 'subscriber',
      ordList: endData,
    })
  }
}
function LabelSubscriber(nowData) {
  var allData = $('#' + nowData.id).attr('data')
}

export { Label, LabelEvent, LabelSubscriber }
